package Offer.offer2018;

/**
 * 替换空格
 * Created by 雷文康 on 2018/3/3.
 */
public class Test04 {
    public static void main(String[] args) {

        System.out.println(replaceBlank("we are happy"));

        System.out.println("we are happy".replace(" ", "20%"));
    }

    public static String replaceBlank(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        char[] ch = str.toCharArray();
        int blankCount = 0;
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] == ' ') {
                blankCount++;
            }
        }
        char[] newCh = new char[2 * blankCount + ch.length];
        int j = 0;
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] != ' ') {
                newCh[j++] = ch[i];
            } else {
                newCh[j++] = '2';
                newCh[j++] = '0';
                newCh[j++] = '%';
            }
        }
        return new String(newCh);
    }
}
